package com.clay.appointment.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clay.appointment.common.exception.ExceptionEnum;
import com.clay.appointment.common.exception.GlobalException;
import com.clay.appointment.common.page.PageInfo;
import com.clay.appointment.entity.hospital.HospitalSet;
import com.clay.appointment.helper.HttpRequestHelper;
import com.clay.appointment.mapper.HospitalSetMapper;
import com.clay.appointment.service.HospitalSetService;
import com.clay.appointment.utils.MD5Utils;
import com.clay.appointment.vo.hospital.HospitalSetQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author : clay
 * @Description: 医院设置服务层实现
 * @Date: Create in 17:01 2021/7/20
 * @Modified By: clay
 */
@Service
@Slf4j
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {
    @Resource
    private HospitalSetMapper hospitalSetMapper;
    @Autowired
    private HospitalSetService hospitalSetService;

    @Override
//    @Transactional(rollbackFor = GlobalException.class, noRollbackFor = RuntimeException.class)
    public PageInfo<HospitalSet> findPageHospitalSet(HospitalSetQueryVO hospitalSetQueryVo) {
//        try {
//            hospitalSetService.findAllHospitalSet();
//            GlobalException.throwOut();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        //创建分页对象，传递当前页，每页记录数
//        Page<HospitalSet> hospitalSetPage = new Page<>(current, limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hospitalName = hospitalSetQueryVo.getHospitalName();
        String hospitalCode = hospitalSetQueryVo.getHospitalCode();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(hospitalName)) {
            wrapper.like("hospital_name", hospitalName);
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(hospitalCode)) {
            wrapper.eq("hospital_code", hospitalCode);
        }
        Page<HospitalSet> page = new Page<>(hospitalSetQueryVo.getPageNumber(), hospitalSetQueryVo.getPageSize());
        hospitalSetMapper.selectPage(page, wrapper);
        return PageInfo.buildPage(page, HospitalSet.class);
    }

    @Override
//    @Transactional(propagation = Propagation.MANDATORY)
    public List<HospitalSet> findAllHospitalSet() {
        return hospitalSetMapper.selectList(null);
    }

    @Override
    public Integer removeHospitalSetById(Long id) {
        return hospitalSetMapper.deleteById(id);
    }

    @Override
    public Integer saveHospitalSet(HospitalSet hospitalSet) {
        //设置状态true使用 false不可用
        hospitalSet.setStatus(true);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5Utils.encode(System.currentTimeMillis() + "" + random.nextInt(1000)));

        return hospitalSetMapper.insert(hospitalSet);
    }

    @Override
    public Integer lockHospitalSet(Long id, boolean status) {
        HospitalSet hospitalSet = hospitalSetMapper.selectById(id);
        //设置状态
        hospitalSet.setStatus(status);
        return hospitalSetMapper.updateById(hospitalSet);
    }

    @Override
    public Integer sendKeyById(Long id) {
        return null;
    }

    @Override
    public Integer batchRemoveHospitalSet(List<Long> list) {
        return hospitalSetMapper.deleteBatchIds(list);
    }

    @Override
    public Integer updateHospitalSetById(HospitalSet hospitalSet) {

        return hospitalSetMapper.updateById(hospitalSet);
    }


    @Override
    public String getSignKey(String hospitalCode) {

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hospital_code", hospitalCode);
        HospitalSet hospitalSet = hospitalSetMapper.selectOne(wrapper);
        return hospitalSet.getSignKey();
    }

    @Override
    public Map<String, Object> JudgSign(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院系统传递过来的签名，签名已进行MD5加密
        String sign = (String) paramMap.get("sign");

        //获取医院编号
        String hospitalCode = (String) paramMap.get("hospitalCode");

        String signKey2 = hospitalSetService.getSignKey(hospitalCode);
        if (!sign.equals(MD5Utils.encode(signKey2))) {
            GlobalException.throwOut(ExceptionEnum.SIGN_ERROR);
        }
        return paramMap;
    }

    @Override
    public Boolean JudgeSign(String sign, String hospitalCode) {
        String signKey2 = hospitalSetService.getSignKey(hospitalCode);
        if (!sign.equals(MD5Utils.encode(signKey2))) {
            GlobalException.throwOut(ExceptionEnum.SIGN_ERROR);
        }
        return true;
    }

}
